<template>
  <div class="patient-number-date">
    <div class="patient-number-date-inside">
      <div class="header">
        <div class="left item">
          <img
              alt="左侧"
              src="@/components/assets/images/design/1-12.webp"
          />
        </div>
        <div class="middle item" @click="handleClick2">请点击下方卡片选择挂号科室</div>
        <div class="right item">
          <img
              alt="右侧"
              src="@/components/assets/images/design/1-12.webp"
          />
        </div>
      </div>
      <el-row
          :gutter="30"
          class="content-area"
          :class="{'content-area-even': (deptOptions?.result?.length)%2===0}"
      >
        <template v-if="deptOptions.state">
          <template v-for="item in (deptOptions.result)">
            <el-col :span="12" @click.native="handleClick(item)">
              <div class="item">{{ item.deptName }}</div>
            </el-col>
          </template>
        </template>
        <template v-else>
          <el-empty description="暂无数据"></el-empty>
        </template>
      </el-row>
    </div>
  </div>
</template>

<script>
import {buildParentApi} from "@/utils/designer/util";
import {v4 as uuid} from "uuid";

let buildParent = buildParentApi();
export default {
  name: 'PatientDept',

  componentName: "PatientDept",

  components: {},

  data() {
    return {}
  },

  computed: {
    // 可选科室
    deptOptions() {
      return this.$store.getters['DRCbiz/getDeptOptions']
    },

    PTypeInfo() {
      return this.$store.getters['DRCbiz/getOPTypeInfo']
    },

    bizInfo() {
      return this.$store.getters['DRCbiz/getBizInfo']
    },

    patientInfo() {
      return this.$store.getters['DRCbiz/getPatientInfo']
    },

    cardTypeInfo() {
      return this.$store.getters['DRCbiz/getCardTypeInfo']
    },

    hospAreaInfo() {
      return this.$store.getters['DRCbiz/getHospAreaInfo']
    },

    DrScheduleInfo() {
      return this.$store.getters['DRCbiz/getDrScheduleList'];
    },

    autoPreAppointReg() {
      return this.$store.getters['DRCbiz/getAutoPreAppointReg']
    },
  },

  mounted() {
  },

  methods: {
    handleClick2() {
      let str = '<?xml version="1.0" encoding="UTF-16LE" standalone="no"?><Response><ResultCode>0</ResultCode><ErrorMsg></ErrorMsg><ResultData><RecordList><RecordInfo><medDate>2025-03-06</medDate><deptCode>134</deptCode><deptName>风湿免疫科专家门诊</deptName><parentdeptCode>144</parentdeptCode><parentdeptName>皮肤科门诊</parentdeptName><doctCode>0397</doctCode><doctName>袁敏芳</doctName><doctTech>风湿免疫科专家门诊</doctTech><medAmPm>2</medAmPm><regfee>0</regfee><treatfee>2500</treatfee><regAmount>2500</regAmount><scheduleId>5|2|134|0397</scheduleId><restnum>10</restnum></RecordInfo></RecordList></ResultData></Response>';
      window.AndroidToWeb('QuerySchedule', str);
    },

    // 点击科室
    handleClick(row) {
      this.QuerySchedule(row);
    },

    // 医生排班
    QuerySchedule(row) {
      const self = this;
      let PTypeInfo = (this.PTypeInfo);
      let bizInfo = (this.bizInfo);
      let patientInfo = (this.patientInfo);
      let hospAreaInfo = (this.hospAreaInfo);
      // 存储选中的科室
      this.$store.dispatch('DRCbiz/setDeptInfo', row);
      let params = Object();
      let paramInfo = Object();
      // 2专家(医生排班--> 预结算信息-->结算结果)&1普通(号源排班--> 预结算信息-->结果)
      let devInfo = localStorage.getItem('deviceInfo');
      Reflect.set(paramInfo, 'deviceIp', (devInfo?.screenIp));
      Reflect.set(paramInfo, 'flowId', uuid());
      Reflect.set(paramInfo, 'timestamp', self.$day().valueOf());
      Reflect.set(paramInfo, 'terminalNo', (devInfo?.screenId));
      Reflect.set(paramInfo, 'method', 'QuerySchedule');
      Reflect.set(paramInfo, 'hospCode', (hospAreaInfo?.id) || 1);
      // 1:预约&2:挂号
      Reflect.set(paramInfo, 'regMode', (bizInfo.id));
      Reflect.set(paramInfo, 'regType', (PTypeInfo.level));
      Reflect.set(paramInfo, 'deptCode', (row.deptCode));
      Reflect.set(paramInfo, 'parentDeptCode', (row?.parentDeptCode));
      Reflect.set(paramInfo, 'sourceCode', 2);
      // 制定查询时间，挂号是当天，预约自己控制查询时间
      if (parseInt(bizInfo.id) === 1) {
        let currDateInfo = self.$store.getters['DRCbiz/getDateInfo'];
        Reflect.set(paramInfo, 'startDate', currDateInfo?.dateInfo);
        Reflect.set(paramInfo, 'endDate', currDateInfo?.dateInfo);
      } else if (parseInt(bizInfo.id) === 2) {
        Reflect.set(paramInfo, 'startDate', this.$day().format('YYYY-MM-DD'));
        Reflect.set(paramInfo, 'endDate', this.$day().format('YYYY-MM-DD'));
      }
      if (patientInfo.state) {
        Reflect.set(paramInfo, 'patientId', (patientInfo.result.patientId));
      } else {
        Reflect.deleteProperty(paramInfo, 'patientId');
      }
      Reflect.set(params, 'Request', paramInfo);
      let paramsInfo = this.$x2js.js2xml(params);
      console.log(params)
      console.log(paramsInfo)
      buildParent.executeParentMethod('axiosRep', paramsInfo, 'QuerySchedule');
    },

    // 预结算业务
    PreAppointReg(row) {
      console.log('***预结算业务***')
      const self = this;
      let PTypeInfo = (this.PTypeInfo);
      let bizInfo = (this.bizInfo);
      let patientInfo = (this.patientInfo);
      let cardTypeInfo = (this.cardTypeInfo);
      let hospAreaInfo = (this.hospAreaInfo);
      // 医生排班信息
      let {state, result = Object()} = this.$store.getters['DRCbiz/getDrScheduleList'];
      // 存储选中的排班
      this.$store.dispatch('DRCbiz/setCurrentScheduleInfo', row);
      let params = Object();
      let paramInfo = Object();
      // 2专家(医生排班--> 预结算信息-->结算结果)&1普通(号源排班--> 预结算信息-->结果)
      let devInfo = localStorage.getItem('deviceInfo');
      Reflect.set(paramInfo, 'deviceIp', (devInfo?.screenIp));
      Reflect.set(paramInfo, 'flowId', uuid());
      Reflect.set(paramInfo, 'timestamp', self.$day().valueOf());
      Reflect.set(paramInfo, 'terminalNo', (devInfo?.screenId));
      Reflect.set(paramInfo, 'method', 'preAppointReg');
      Reflect.set(paramInfo, 'hospCode', (hospAreaInfo?.id) || 1);
      // 电子医保凭证
      if (parseInt(cardTypeInfo?.id) === 29) {
        Reflect.set(paramInfo, 'cardNo', (cardTypeInfo?.cardInfo));
        Reflect.set(paramInfo, 'cardHardInfo', (cardTypeInfo?.cardInfo));
      } else {
        Reflect.set(paramInfo, 'cardNo', (cardTypeInfo?.cardNo));
        Reflect.set(paramInfo, 'cardHardInfo', (cardTypeInfo?.cardInfo));
      }
      Reflect.set(paramInfo, 'cardType', (cardTypeInfo.id));
      Reflect.set(paramInfo, 'regMode', (bizInfo.id));
      Reflect.set(paramInfo, 'regType', (PTypeInfo.level));

      // 就诊日期medDate--制定查询时间，挂号是当天，预约自己控制查询时间
      // 就诊时间medTime--挂号可以为空
      if (parseInt(bizInfo.id) === 1) {
        let currDateInfo = self.$store.getters['DRCbiz/getDateInfo'];
        Reflect.set(paramInfo, 'medDate', currDateInfo?.dateInfo);
      } else if (parseInt(bizInfo.id) === 2) {
        Reflect.set(paramInfo, 'medDate', this.$day().format('YYYY-MM-DD'));
      }
      if (patientInfo.state) {
        Reflect.set(paramInfo, 'patientId', (patientInfo?.result?.patientId));
      } else {
        Reflect.set(paramInfo, 'patientId', '');
      }
      Reflect.set(paramInfo, 'deptCode', (result?.deptCode));
      Reflect.set(paramInfo, 'scheduleId', (result?.scheduleId));
      Reflect.set(paramInfo, 'doctCode', (result?.doctCode));
      Reflect.set(paramInfo, 'medAmPm', (result?.medAmPm) || 1);

      // 数据需要确认
      Reflect.set(paramInfo, 'cash', (result?.regAmount));
      Reflect.set(paramInfo, 'ybCardNo', null);
      Reflect.set(paramInfo, 'isZj', 0);
      Reflect.set(paramInfo, 'TOKEN', null);
      Reflect.set(paramInfo, 'deviceVersion', uuid());
      Reflect.set(paramInfo, 'deviceInfo', uuid());
      Reflect.set(paramInfo, 'operId', uuid());
      Reflect.set(paramInfo, 'sourceCode', 2);
      Reflect.set(paramInfo, 'tradeTime', this.$day().format('YYYY-MM-DD HH:mm:ss'));
      Reflect.set(params, 'Request', paramInfo);
      let paramsInfo = this.$x2js.js2xml(params);
      buildParent.executeParentMethod('axiosRep', paramsInfo, 'preAppointReg');
    },
  },


  watch: {
    // 是否自动获取预结算的信息
    autoPreAppointReg: {
      handler: function (val) {
        let bizInfo = this.$store.getters['DRCbiz/getBizInfo'];
        let OPTypeInfo = this.$store.getters['DRCbiz/getOPTypeInfo'];
        let {state, result = Object()} = (this.DrScheduleInfo);
        console.log('************')
        console.log(state);
        console.log(result);
        console.log('************')
        // 排班信息请求成功并且挂号业务欧且是普通号
        if (val && ((bizInfo?.routeName === 'register') && (OPTypeInfo?.id === 1)) && !!state) {
          this.PreAppointReg(result);
        }
      },
      deep: true,
      immediate: true,
    }
  }
}

</script>

<style scoped lang="scss">
@import "./assets/style";
</style>